package com.oig.sys.workflow.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.workflow.dao.ITbAutoRuleDao;
import com.oig.sys.workflow.dto.AutoRuleDto;
import com.oig.sys.workflow.entity.TbAutoRule;
import com.oig.sys.workflow.service.ITbAutoOperationService;
import com.oig.sys.workflow.service.ITbAutoRuleService;
import com.oig.sys.workflow.vo.AutoOperationVo;
import com.oig.sys.workflow.vo.AutoRuleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuxl
 */
@Service
public class TbAutoRuleServiceImpl implements ITbAutoRuleService {

	private final ITbAutoRuleDao tbAutoRuleDao;
	private final ITbAutoOperationService autoOperationService;

	@Autowired
	public TbAutoRuleServiceImpl(ITbAutoRuleDao tbAutoRuleDao,
								 ITbAutoOperationService autoOperationService){
		 this.tbAutoRuleDao = tbAutoRuleDao;
		 this.autoOperationService = autoOperationService;
	}

	@Transactional
	@Override
	public AutoRuleVo addAutoRule(AutoRuleDto autoRuleDto){
		TbAutoRule autoRule = new TbAutoRule();
		BeanUtils.copyProperties(autoRuleDto, autoRule,"id");

		autoRule.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
		autoRule.setEnable(CommonEnum.FlagEnum.TRUE_FLAG.getCode());
		this.tbAutoRuleDao.save(autoRule) ;
		List<AutoOperationVo> autoOperationVoList=autoOperationService.addAutoOperationList(autoRule, autoRuleDto.getAutoOperationDtoList()) ;
		AutoRuleVo autoRuleVo = this.buildVo(autoRule) ;
		autoRuleVo.setAutoOperationVoList(autoOperationVoList);
		return autoRuleVo;
	}

	@Transactional
	@Override
	public AutoRuleVo updateAutoRule(AutoRuleDto autoRuleDto){
		TbAutoRule autoRule = this.tbAutoRuleDao.getTbAutoRuleById(autoRuleDto.getId()) ;
		if (!CommonEnum.CommonStatusEnum.OK.getCode().equals(autoRule.getStatus())){
			throw new BadRequestException("规则已删除") ;
		}
		BeanUtils.copyProperties(autoRuleDto, autoRule, "id","createUserId","createUserName","createTime","tenantId","tenantName", "enable");
		this.tbAutoRuleDao.save(autoRule) ;
		List<AutoOperationVo> autoOperationVoList=autoOperationService.updateAutoOperationList(autoRule, autoRuleDto.getAutoOperationDtoList()) ;
		AutoRuleVo autoRuleVo = this.buildVo(autoRule) ;
		autoRuleVo.setAutoOperationVoList(autoOperationVoList);
		return autoRuleVo;
	}

	@Override
	public AutoRuleVo getAutoRule(Integer id) {
		TbAutoRule autoRule = this.tbAutoRuleDao.getTbAutoRuleById(id) ;
		AutoRuleVo ruleVo = this.buildVo(autoRule) ;
		ruleVo.setAutoOperationVoList(this.autoOperationService.findByRuleId(id));
		return ruleVo;
	}

	@Transactional
	@Override
	public void cancelAutoRule(Integer id){
		TbAutoRule autoRule = this.tbAutoRuleDao.getTbAutoRuleById(id) ;
		autoRule.setStatus(CommonEnum.CommonStatusEnum.DEL.getCode());
		autoRule.setEnable(CommonEnum.FlagEnum.FALSE_FLAG.getCode());
		this.tbAutoRuleDao.save(autoRule);
	}

	@Transactional
	@Override
	public void delAutoRule(Integer id) {
		TbAutoRule autoRule = this.tbAutoRuleDao.getTbAutoRuleById(id) ;
		if (!CommonEnum.CommonStatusEnum.DEL.getCode().equals(autoRule.getStatus())){
			throw new BadRequestException("请先废弃规则，再彻底清除") ;
		}
		this.autoOperationService.deleteByRuleId(id);
		this.tbAutoRuleDao.delete(autoRule);
	}

	@Transactional
	@Override
	public void disableAutoRule(Integer id) {
		TbAutoRule autoRule = this.tbAutoRuleDao.getTbAutoRuleById(id) ;
		if (CommonEnum.FlagEnum.FALSE_FLAG.getCode().equals(autoRule.getEnable())){
			throw new BadRequestException("本身已停用") ;
		}
		autoRule.setEnable(CommonEnum.FlagEnum.FALSE_FLAG.getCode());
		this.tbAutoRuleDao.save(autoRule);
	}
	@Transactional
	@Override
	public void enableAutoRule(Integer id) {
		TbAutoRule autoRule = this.tbAutoRuleDao.getTbAutoRuleById(id) ;
		if (CommonEnum.CommonStatusEnum.DEL.getCode().equals(autoRule.getStatus())){
			throw new BadRequestException("已删除规则，不能启动");
		}
		if (CommonEnum.FlagEnum.TRUE_FLAG.getCode().equals(autoRule.getEnable())){
			throw new BadRequestException("本身已是启用") ;
		}
		autoRule.setEnable(CommonEnum.FlagEnum.TRUE_FLAG.getCode());
		this.tbAutoRuleDao.save(autoRule);
	}

	private AutoRuleVo buildVo(TbAutoRule autoRule){
		AutoRuleVo autoRuleVo = new AutoRuleVo();
		BeanUtils.copyProperties(autoRule, autoRuleVo);
		return autoRuleVo;
	}

	@Override
	public List<AutoRuleVo> findAutoRule(AutoRuleDto autoRuleDto) {
		List<AutoRuleVo> autoRuleVoList = new ArrayList<>();
		List<TbAutoRule> autoRules = this.tbAutoRuleDao.findAllEnableRule(
				autoRuleDto.getListenerTableName(), autoRuleDto.getListenerEventOperation());
		if (CollectionUtil.isNotEmpty(autoRules)){
			autoRules.forEach((item)->autoRuleVoList.add(this.buildVo(item)));
		}
		return autoRuleVoList;
	}


	@Override
	public List<Integer> findOkRuleIdByObjectId(Integer objectId) {
		return this.tbAutoRuleDao.findIdByListenerObjectId(objectId, CommonEnum.CommonStatusEnum.OK.getCode()) ;
	}

	@Override
	public List<Integer> findRuleIdByObjectId(Integer objectId) {
		return this.tbAutoRuleDao.findIdByListenerObjectId(objectId) ;
	}

	@Override
	public List<Integer> findRuleIdByEventIdList(List<Integer> eventIdList) {
		return this.tbAutoRuleDao.findIdByEventIdList(eventIdList);
	}
}
